package com.luojie.service.impl;


import com.luojie.constant.MessageConstant;
import com.luojie.constant.StatusConstant;
import com.luojie.dto.EmployeeDTO;
import com.luojie.exception.AccountLockedException;
import com.luojie.exception.AccountNotFoundException;
import com.luojie.exception.PasswordErrorException;
import com.luojie.mapper.EmployeeMapper;
import com.luojie.po.Employee;
import com.luojie.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;


@Service
public class EmployeeServiceImpl implements EmployeeService {
    @Autowired
    private EmployeeMapper employeeMapper;

    @Override
    public Employee login(EmployeeDTO employeeDTO) {
        String username = employeeDTO.getUsername();
        String password = employeeDTO.getPassword();
        Employee employee = employeeMapper.getByUsername(username);
        if (employee == null) {
            throw new AccountNotFoundException(MessageConstant.ACCOUNT_NOT_FOUND);
        }
        //密码比对
        // 对我们前端传过来的明文密码进行MD5加密处理
       //TODO MD5 加密
        password= DigestUtils.md5DigestAsHex(password.getBytes());
        if (!password.equals(employee.getPassword())) {
            //密码错误
            throw new PasswordErrorException(MessageConstant.PASSWORD_ERROR);
        }

        if (employee.getStatus() == StatusConstant.DISABLE) {
            //账号被锁定
            throw new AccountLockedException(MessageConstant.ACCOUNT_LOCKED);
        }
        return employee;
    }
}
